package http服务端.生命周期_7;

import java.net.URI;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

	@Override
	protected void channelRead0(ChannelHandlerContext arg0, HttpObject arg1) throws Exception {//4
		// TODO Auto-generated method stub
		
		System.out.println(arg1.getClass());//输出arg1可能的类名
		
		System.out.println(arg0.channel().remoteAddress());//输出远程连接地址及端口号
		
		if (arg1 instanceof HttpRequest) {
			HttpRequest httpRequest = (HttpRequest) arg1;
			
			System.out.println("请求方法名：" + httpRequest.method().name());
			
			URI uri = new URI(httpRequest.uri());
			if("/favicon.ico".equals(uri.getPath())) {
				System.out.println("请求favicon.ico");
				return;
			}
			
//			System.out.println("执行channelHead0");
			
			ByteBuf content = Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8);
			FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, 
					HttpResponseStatus.OK, content);
			response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
			response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
			
			arg0.writeAndFlush(response);
			arg0.close();
		}
	}
	
	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {//1
		// TODO Auto-generated method stub
		System.out.println("handlerAdded  -  handler added");
		super.handlerAdded(ctx);
	}
	
	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {//2
		// TODO Auto-generated method stub
		System.out.println("channelRegistered  -  channel registered");
		super.channelRegistered(ctx);
	}
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {//3
		// TODO Auto-generated method stub
		System.out.println("channelActive  -  channel active");
		super.channelActive(ctx);
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {//5
		// TODO Auto-generated method stub
		System.out.println("channelInactive  -  channel inactive");
		super.channelInactive(ctx);
	}
	
	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {//6
		// TODO Auto-generated method stub
		System.out.println("channelUnregistered  -  channel unregistered");
		super.channelUnregistered(ctx);
	}
	
	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {//7
		// TODO Auto-generated method stub
		System.out.println("handlerRemoved  -  channel removed");
		super.handlerRemoved(ctx);
	}
}
